package Convertisseur;

import i18n.LanguageChooser;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import jxl.Sheet;

import com.mxgraph.util.mxResources;

import DataStorage.CodageCheminDAOHibernate;
import DataStorage.EchantillonDAOHibernate;
import HibernateSQLite.java.model.CodageChemin;
import HibernateSQLite.java.model.Echantillon;

public class VerifierMetaReglesDeCoherenceEchantillon extends Verification {

	private Map<String, Integer> mapOuils = new HashMap<String, Integer>();
	private static List<String> listMessageJugement = new ArrayList<String>();
	private static Map<String, String> map_OP_message = new HashMap<String,String>();
	

	
	public List<String> getListMessageJugement() {
		return listMessageJugement;
	}
	
	

	public static Map<String, String> getMap_OP_message() {
		return map_OP_message;
	}



	@SuppressWarnings({ "rawtypes", "unchecked" })
	public void verifierMetaRegle() {
		
		listMessageJugement.clear();
		map_OP_message.clear();

		Map<String, List<String>> mapDB = getMap_MetaReglesByDB();
		for (String OP : listCodageChemin) {
			//int nbOutilByOperateur = getNumberOutilByDB(OP);
			Map map = this.classificationOP();
			List outilsOP = (List) map.get(OP);
			// int nbOutil = mapDB.get(OP).size() - 1;

			
			calculerNumberSameOutilOfOP(outilsOP, OP);

			// ------------------
			// jugement
			// ------------------
			String outil = "";
			/*
			String N_serie_bras ="";
			String identificationPiece =  getIdentificationPiece(sheet);
			//essieu
			if(identificationPiece.contains("ch")){
				N_serie_bras = this.getN_serie(sheet);
			}
			
			//bras
			if(identificationPiece.contains("bras")){
				N_serie_bras = this.getBras(sheet);
			}
			*/		
			// outil = 0, [0]
			if (mapDB.get(OP).size() == 1 && mapDB.get(OP).contains("0")) {
				outil = mapDB.get(OP).get(0); // outil =0 ex: OP=120, outil=0
				String message = this.getMessageOfJugement(OP, outil);
				if (!message.equals("")) {
					listMessageJugement.add(message);
					if(map_OP_message.containsKey(OP)){
						message = message +"," +map_OP_message.get(OP);
						map_OP_message.put(OP, message);
					}else{
						map_OP_message.put(OP, message);
					}
				}
			}
			// outil != 0, [0,1] [0,1,2,3]
			if (mapDB.get(OP).size() > 1 && mapDB.get(OP).contains("0")) {
				// [0,1]
				if ((mapDB.get(OP).size() - 1) == 1) {
					for (String outils : mapDB.get(OP)) {
						if (!outils.equals("0")) {
							outil = outils; // outil =0 ex: OP=120, outil=0
							String message = this.getMessageOfJugement(OP,outil);
							if (!message.equals("")) {
								listMessageJugement.add(message);
								if(map_OP_message.containsKey(OP)){
									message = message +"," +map_OP_message.get(OP);
									map_OP_message.put(OP, message);
								}else{
									map_OP_message.put(OP, message);
								}
							}
						}
					}
				}
				// [0,1,2,3]
				if ((mapDB.get(OP).size() - 1) > 1) {
					for (String outils : mapDB.get(OP)) {
						if (!outils.equals("0")) {
							outil = outils; // outil =0 ex: OP=120, outil=0
							
							String message = this.getMessageOfJugement(OP,outil);
							if (!message.equals("")) {
								listMessageJugement.add(message);
								if(map_OP_message.containsKey(OP)){
									message = message +"," +map_OP_message.get(OP);
									map_OP_message.put(OP, message);
								}else{
									map_OP_message.put(OP, message);
								}
							}
						}
					}
				}

			}
		}
		//return listMessageJugement;
	}
	public String getMessageOfJugement(String OP, String outil) {
		
		
		String Message_MetaRegles = "";		
		//String message = mxResources.get("error1");
		// --------------
		// get message
		// --------------
		String opOutil = OP + outil;
		
		if (mapOuils.containsKey(opOutil)) {
			
			if (mapOuils.get(opOutil)<2) {
				//lancer fonction pour  chercher les message error
				LanguageChooser languageChooser = new LanguageChooser();
				languageChooser.addResources();
				//outil de OP < 2 fois
				Message_MetaRegles = opOutil+" "+ mxResources.get("error3");
			}
		} else {
			//lancer fonction pour  chercher les message error
			LanguageChooser languageChooser = new LanguageChooser();
			languageChooser.addResources();
			//n'exist pas cet outil dans fichier echantillon Macro
			Message_MetaRegles = opOutil+" "+ mxResources.get("error4");
		}
		// --------------
		// End message
		// --------------

		return Message_MetaRegles;
	}
	//-------------------------------------------------------
	//	example X95 
	//	nombre essieu:  4
	//	chemin: 330, 110, 330, 110
	//  list_ToutOPChemin:   [[3, 3, 0], [1, 1, 0], [1, 1, 0], [3, 3, 0]]
	//
	//	OP: 		[ 130, |	140,  |	170 ]
	//	chemin:		[ 0,1,3|	0,1,3 |	0	]
	//
	//	parametre : List: [3, 1, 1, 3],[3, 1, 1, 3],[0, 0, 0, 0]
	//				OP	: 130,	140,	170
	//
	//  resultat : mapOuils  	<1301, 2*fois>, <1303, 2*fois>, 
	//							<1401, 2*fois>, <1403, 2*fois>,  
	//							<1700, 4*fois>
	//-------------------------------------------------------
	public void calculerNumberSameOutilOfOP(List<Object> list, String OP) {
		String value = "";
		String outil = "";
		int count = 0;
		for (Object i : list) {
			value = (String) i;
			outil = value;
			count = 0;
			for (Object j : list) {
				if (value.equals(j)) {
					count++;
				}
			}
			
			mapOuils.put(OP + outil, count);
		}
	}
	
	//------------------------------------------------------------------------------------
	//	example X95 
	//	nombre essieu:  4
	//	chemin: 330, 110, 330, 110
	//  list_ToutOPChemin:   [[3, 3, 0], [1, 1, 0], [1, 1, 0], [3, 3, 0]]
	//
	//	OP: 		[ 130, |	140,  |	170 ]
	//	chemin:		[ 0,1,3|	0,1,3 |	0	]
	//
	//	return map<OP,outil>
	//  example : map<130,[3, 1, 1, 3]>
	//  example : map<140,[3, 1, 1, 3]>
	//  example : map<170,[0, 0, 0, 0]>
	//-------------------------------------------------------------------------------------
	//-------------------------------------------------------------------------------------
	//	example X61 -
	//	nombre essieu:  4; 
	//	nombre Bras:  4;
	//	chemin d'essieu: 579653, 579654, 579655, 579656
	//	chemin de bras: 11, 22, 33, 44
	//
	//	OP d'essieu: 			[ 	110, 	|	130,  |	  160 	| 170 	|  180 	|  190 	]
	//	chemin d'essieu:		[ 0,1,2,3,4	|	0,1,2 |	0,1,2,3 | 0,1,2	| 0,1,2 | 0,1,2	]
	//  OP de bras: 			[ 	100 	| 	110		]
	//  chemin de bras: 		[ 0,1,2,3,4 | 0,1,2,3,4 ]
	//
	//	Essieu: list_ToutOPChemin:		[[3, 2, 3, 2, 1, 2], [4, 2, 1, 2, 1, 2], [2, 1, 3, 1, 2, 1], [1, 1, 2, 1, 2, 1]]
	//	Bras:   list_ToutOPBras:		[[3, 3], [1, 1], [2, 2], [4, 4]]
	//------------------------------------------------------------------------------------


	public Map<String, List<String>> classificationOP() {
		Map<String, List<String>> map = new HashMap<String, List<String>>();
		List<List<String>> listClassificationOP = new ArrayList<List<String>>();
		//essieu
		for (int i = 0; i < listCodageChemin.size(); i++) {
			List<String> outils = new ArrayList<String>();
			System.out.println("list_ToutOPChemin:"+list_ToutOPChemin);
			for (List<String> chemin : list_ToutOPChemin) {
				outils.add(chemin.get(i));
			}
			listClassificationOP.add(outils);
			map.put(listCodageChemin.get(i), outils);
		}
		//bras
		if(!listCodageBras.isEmpty()){
			for(int i=0;i<listCodageBras.size();i++){
				List<String> outils = new ArrayList<String>();
				for (List<String> chemin : list_ToutOPBras) {
					String OP_bras = listCodageBras.get(i);
					if(map.containsKey(OP_bras)){
						outils = map.get(OP_bras);
						outils.add(chemin.get(i));
					}
				}
				listClassificationOP.add(outils);

				map.put(listCodageBras.get(i), outils);
			}
		}		
		return map;
	}
	//-------------------------------------------------------
	//	example X95 
	//	nombre essieu:  4
	//	OP: 		[ 130, |	140,  |	170 ]
	//	chemin:		[ 0,1,3|	0,1,3 |	0	]
	//	OP 130
	//-------------------------------------------------------
	
	public int getNumberOutilByDB(String OP) {
		int nb = 0;
		EchantillonDAOHibernate EchantillonDAO = new EchantillonDAOHibernate();
		Echantillon echantillon = EchantillonDAO
				.getEchantillon_PiecePrincipale(InformationLigne_id);
		int nbEssieu = Integer.parseInt(echantillon.getNombre());
		Map<String, List<String>> map = getMap_MetaReglesByDB();
		if (map.get(OP).size() > 1 && map.get(OP).contains("0")) {
			nb = map.get(OP).size() - 1;
			return (nbEssieu / nb);
		}
		if (map.get(OP).size() == 1 && map.get(OP).contains("0")) {
			return nbEssieu;
		}

		return (Integer) null;
	}
	//-------------------------------------------------------
	//	example X95 
	//	nombre essieu:  4
	//	OP: 		[ 130, |	140,  |	170 ]
	//	chemin:		[ 0,1,3|	0,1,3 |	0	]
	//	retrun map<OP,list outil>, 
	//                     	example --> map<130,[ 0,1,3]>
	//						example --> map<140,[ 0,1,3]>
	//						example --> map<170,[ 0 ]>
	//-------------------------------------------------------
	public Map<String, List<String>> getMap_MetaReglesByDB() {
		CodageCheminDAOHibernate codageCheminDAO = new CodageCheminDAOHibernate();
		List<CodageChemin> listcodageChemin = codageCheminDAO
				.getListCodageChemin(InformationLigne_id);
		Map<String, List<String>> map = new HashMap<String, List<String>>();
		List<String> listOutil = new ArrayList<String>();

		for (CodageChemin codageChemin : listcodageChemin) {
			String op = codageChemin.getOP();
			String outils = codageChemin.getChemin();
			String[] array = outils.split(",");
			listOutil = Arrays.asList(array);
			// listOutil.remove("0");
			map.put(op, listOutil);
		}
		return map;
	}
	public void testMessage(){
		LanguageChooser languageChooser = new LanguageChooser();
		languageChooser.addResources();
		String mesaage1 = mxResources.get("error1");
		String mesaage2 = mxResources.get("error2");
		String mesaage3 = mxResources.get("error3");
		String mesaage4 = mxResources.get("error4");
		System.out.println("mesaage1"+mesaage1);
		System.out.println("mesaage2"+mesaage2);
		System.out.println("mesaage3"+mesaage3);
		System.out.println("mesaage4"+mesaage4);
	}
	
	public static void main(String [] args){
		VerifierMetaReglesDeCoherenceEchantillon t = new VerifierMetaReglesDeCoherenceEchantillon();
		t.testMessage();
	}

}
